﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using System.Data.Entity;
using System.Web.UI.WebControls;
using System.Threading;
using EntityFramework.Extensions;
using System.Transactions;
namespace Webcar.Controllers
{
    public partial class DriverController : BaseControler
    {
        [HttpGet]
        public ActionResult List()
        {
            ViewBag.ShowOptions = new ShowCarOptions() { ShowNormal = true };
            HttpContext.Session["ShowDriverOptions"] = ViewBag.ShowOptions;
            //ViewBag.ShowOptions = new ShowDriverOptions();
            //HttpContext.Session["ShowDriverOptions"] = ViewBag.ShowOptions;
            return View();
        }
        [HttpPost]
        public ActionResult List(ShowCarOptions options)
        {
            ViewBag.ShowOptions = options;
            HttpContext.Session["ShowDriverOptions"] = options;
            return View();
        }
        [HttpGet]
        public ActionResult MotoList()
        {
            //ViewBag.ShowOptions = new ShowDriverOptions() { ShowMoto = true };
            //HttpContext.Session["ShowDriverOptions"] = ViewBag.ShowOptions;
            return View();
        }
        public ActionResult ListPartialView(string id)
        {
            return PartialView();
        }
        public ActionResult Delete(string id)
        {
            CurrentUser.EnsureAdmin();
            var driver = WebcarDataProvider.DB.BS_EMP.Find(id);
            if (driver != null)
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    using (var DB = new WebcarContext())
                    {
                        DB.ObjectContext.ExecuteStoreCommand("update sec_record set driver='" + driver.MAN_NAME + "(已删)' where driver='" + id + "'");
                        DB.ObjectContext.ExecuteStoreCommand("update rp_car_repair_head set driver='" + driver.MAN_NAME + "(已删)v' where driver='" + id + "'");
                        DB.SEC_DRV_STUDY.Delete(a => a.MAN_CODE == id);
                        DB.SaveChanges();
                    }
                    transaction.Complete();
                }
            }

            return RedirectToAction("List", "Driver");
        }
        public ActionResult DeleteStop(string id)
        {
            using (var DB = new WebcarContext())
            {
                var driver = DB.BS_EMP.Find(id);
                if (driver != null)
                {
                    if (driver.IS_USE == "T")
                    {
                        driver.IS_USE = "F";
                    }
                    else if (driver.IS_USE == "F")
                    {
                        driver.IS_USE = "T";
                    }
                }
                DB.SaveChanges();
            }
            return RedirectToAction("List", "Driver");
        }
        public ActionResult Edit(string man_code,string deptcode)
        {
            BS_EMP user = null;
            if (man_code.IsNotNull())
            {
                user = WebcarDataProvider.DB.BS_EMP.Find(man_code);
            }
            if (user == null)
            {
                user = new BS_EMP();
                user.DEPT_CODE = deptcode;
                user.IS_USE = "T";
            }

            var dept = WebcarDataProvider.DB.sys_dept.Find(user.DEPT_CODE);
            if (dept != null)
                ViewBag.DeptName = dept.deptname;

            return PartialView(user);
        }
        [HttpGet]
        public ActionResult EmployeeExam(string id)
        {
            BS_EMP_2 user = null;
            if (id.IsNotNull())
            {
                user = WebcarDataProvider.DB.BS_EMP_2.Find(id);
            }
            if (user == null)
            {
                user = new BS_EMP_2();
                user.MAN_CODE = id;
            }

            BS_EMP user_entity = WebcarDataProvider.DB.BS_EMP.Find(id);
            ViewBag.Driver = user_entity;

            return PartialView(user);
        }
        [HttpPost]
        public ActionResult EmployeeExam(BS_EMP_2 model)
        {
            CurrentUser.EnsurePermission(PermissionManager.编辑驾驶员);
            using (var DB = new WebcarContext())
            {
                BS_EMP_2 item = DB.BS_EMP_2.Find(model.MAN_CODE);
                if (item == null)
                {
                    DB.BS_EMP_2.Add(model);
                }
                else
                {
                    model.CopyPropertyTo(item);
                }
                BS_EMP driver = DB.BS_EMP.Find(model.MAN_CODE);
                if (driver != null)
                {
                    driver.IN_DATE = model.START_DATE;
                }
                DB.SaveChanges();
            }
            return Success("List", "Driver");
        }
        [HttpGet]
        public ActionResult SetCardFlag(string id,bool card_flag)
        {
            string sql = "update bs_emp set is_card='{0}' where man_code='{1}'";
            sql = sql.FormatWith(card_flag?"T":"F", id);
            WebcarDataProvider.DB.ObjectContext.ExecuteStoreCommand(sql);
            return Content("OK");
        }

        bool CheckCarModel(BS_EMP model, ref string s_error)
        {
            if (model.DRIVE_FIRST.IsNullDateTime())
            {
                s_error = "初次领证日期格式有误。";
                return false;
            }
            if (model.BIRTH==null)
            {
                s_error = "生日格式有误。";
                return false;
            }
            if (model.DRIVE_KIND.IsNull())
            {
                s_error ="请录入准驾车型";
                return false;
            }
            if (model.PHONE.IsNull())
            {
                s_error = "请录入电话号码。";
                return false;
            }
            if (model.DEPT_CODE.IsNull())
            {
                s_error = "请录入车辆部门。";
                return false;
            }
            if (model.CHILD_DEPT.IsNull() )
            {
                s_error = "请录入下级部门。";
                return false;
            }
            return true;
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditSave(BS_EMP model)
        {
            try
            {
                CurrentUser.EnsurePermission(PermissionManager.编辑驾驶员);
                string error = "";
                if (this.CheckCarModel(model, ref error) == false)
                {
                    return Content("错误:" + error);
                }
                BS_EMP item = null;
                using (var DB = new WebcarContext())
                {
                    if (model.MAN_CODE.IsNotNull())
                    {
                        item = DB.BS_EMP.Find(model.MAN_CODE);
                    }
                    else
                    {
                        item = new BS_EMP();
                        DB.BS_EMP.Add(item);
                        item.IS_USE = "T";
                    }
                    model.CopyPropertyTo(item);
                    if (item.MAN_CODE.IsNull())
                        item.MAN_CODE = "M" + DB.GetSearialId();
                    DB.SaveChanges();
                }
                return SuccessAlertThenReload();
            }
            catch (Exception ex)
            {
                return this.ErrorContent(ex);
            }
        }


        public ActionResult Export()
        {
            GridViewSettings settings = new GridViewSettings();
            settings.Name = "gridView";
            settings.AddDeptColumn();
            settings.AddChildDeptColumn();
            settings.Columns.Add("MAN_NAME", "姓名");
            settings.Columns.Add("ID", "身份证号");
            settings.AddJobKindColumn();
            settings.AddSexKindColumn();
            settings.Columns.Add("ARCHIVES_NO", "档案号");
            settings.Columns.Add("DRIVE_NO", "驾照号");
            settings.Columns.Add("DRIVE_KIND", "准驾车型");

            settings.Columns.Add("IN_DATE", "聘用日期", MVCxGridViewColumnType.DateEdit).PropertiesEdit.DisplayFormatString = "yyyy-MM-dd";
            settings.AddClassLevelColumn();
            //settings.AddYesNoColumn
            settings.AddCarStatusColumn();
            var data = UserDataProvider.GetDriverList(this.RouteData).ToDataTable();
            return GridViewExtension.ExportToXls(settings, data, "驾驶员档案");

            //return RedirectToAction("List", "Car");
        }
    }


    //public class ShowDriverOptions
    //{
    //    public bool ShowNormal
    //    {
    //        get;
    //        set;
    //    }
    //    public bool ShowDelete
    //    {
    //        get;
    //        set;
    //    }
    //    public bool ShowDiscard
    //    {
    //        get;
    //        set;
    //    }
    //    public bool ShowMoto
    //    {
    //        get;
    //        set;
    //    }
    //}
}